<?php
/**
 * Created by PhpStorm.
<<<<<<< Updated upstream
 * User: lenovo
 * Date: 2018/12/12
 * Time: 17:07
=======
 * User: LiuJiaJun
 * Date: 2018/12/25
 * Time: 11:32
>>>>>>> Stashed changes
 */

namespace api\storage\model;


use think\Model;

class SeatModel extends Model
{

    protected $table = 'iwh_seat';
    protected $pk = 'uuid';

    /**
     * 根据seatCode 和 uuid 获取站位信息
     *
     * @param string $seatCode
     * @param string $storageUuid
     * @return array|false|\PDOStatement|string|Model
     * 根据条件获取seatId
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSeatId($seatCode = '',$storageUuid = ''){
        $where = ['seat_code'=>$seatCode, 'storage_uuid'=>$storageUuid];
            $result = $this->where($where)->field('seat_id')->select();
            return $result;
    }

    /**
     * 更新seatId
     *
     * @param $seatCode
     * @param $uuid
     * @param $data
     * @return false|int
     */
    public function updateSeatId($seatCode,$uuid,$data){
        $where['storage_uuid'] = $uuid;
        $where['seat_code'] = $seatCode;
        return $this->isUpdate(true)->save($data,$where);
    }

    /**
     * 根据seatCode 和  storageUuid 来更新数据
     *
     * @param $seatCode
     * @param $storageUuid
     * @param $data
     * @return false|int
     */
    public function updateByCodeAndUuid($seatCode,$storageUuid,$data){
        $where['seat_code'] = $seatCode;
        $where['storage_uuid'] = $storageUuid;
        return $this->isUpdate(true)->save($data,$where);
    }

    /**
     * 更新仓库占用
     *
     * @param $seatCode
     * @param $storageUuid
     * @return false|int
     */
    public function updateIsUsed($seatCode,$storageUuid){
        $where['seat_code'] = $seatCode;
        $where['storage_uuid'] = $storageUuid;
        $data['is_used'] = 1;
        return $this->isUpdate(true)->save($data,$where);
    }

    /**
     * 批量更新仓库占用字段
     *
     * @param $seatCodeList
     * @param $storageUuid
     * @return false|int
     */
    public function updateBatchNotUsed($seatCodeList,$storageUuid){
        if(is_string($seatCodeList)){
            $seatCodeList = [$seatCodeList];
        }
        $where['storage_uuid'] = $storageUuid;
        $where['seat_code'] = ['in',$seatCodeList];
        return $this->isUpdate(true)
            ->save(['is_used'=>0],$where);
    }

    /**
     * 更新站位命令码
     *
     * @param $seatId
     * @param $storageUuid
     * @param $commandCode
     * @return false|int
     */
    public function updateCommandCode($seatId,$storageUuid,$commandCode){
        $data['command_code'] = $commandCode;
        $where['seat_id'] = $seatId;
        $where['storage_uuid'] = $storageUuid;
        return $this->isUpdate(true)->save($data,$where);
    }


    /** 扫描站位的货架信息--仓储员、货架号
     * @param $seatCode
     * @param $storageUuid
     * @return array|false|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */

    public function getShelvesInfo($seatCode = '',$storageUuid = ''){
        return $this->alias('S')
            ->join('iwh_shelves sh','S.shelves_uuid = SH.uuid','INNER')
            ->join('cmf_users U','U.uuid = SH.staff_uuid','LEFT')
            ->field('SH.uuid,SH.shelves_code,SH.staff_uuid,U.user_login ,SH.flag')
            ->where(['S.seat_code'=>$seatCode,
                'S.storage_uuid'=>$storageUuid])
            ->find();

    }

    /**
     * 找出seatId
     *
     * @param $seatCode
     * @param $uuid
     * @return mixed
     */
    public function getSeatIdByCodeAndUuid($seatCode,$uuid){
        $where['seat_code'] = $seatCode;
        $where['storage_uuid'] = $uuid;
        return $this->where($where)->value('seat_id');
    }

    /**
     * 根据seatCode 和 uuid 获取站位id的信息
     *
     * @param $seatCode
     * @param $uuid
     * @return array
     */
    public function findSeatIdByCodeAndStorageUuid($seatCode,$uuid){
        return $this->where('seat_code',$seatCode)->where('storage_uuid',$uuid)->value('seat_id');
    }

    /**
     * 一次性查找seatId
     *
     * @param $seatCodes
     * @param $uuid
     * @return array
     */
    public function querySeatId($seatCodes,$uuid){
        return $this->where('storage_uuid',$uuid)
            ->whereIn('seat_code',$seatCodes)
            ->column('seat_id');
    }

    /**
     *
     *
     * @param $seatCode
     * @param $uuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function findByCodeAndStorageUuid($seatCode,$uuid){
        return $this->where('seat_code',$seatCode)->where('storage_uuid',$uuid)->find();
    }

    public function getAllSeatId($where){

        $result = $this->distinct(true)->field('LEFT(seat_id,4) as seat_id')->where($where)->select();
        return $result;
    }

    /**
     * 根据货架的uuid查找站位
     *
     * @param $uuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function findSeatCodeByShelfUuid($uuid){
        $where['status'] = 0;
        $where['is_used'] = 0;
        $where['shelves_uuid'] = $uuid;
        return $this->field('seat_code,seat_id')
            ->where($where)
            ->order('seat_code asc')
            ->find();
    }

    /**
     * 查询站位的类型
     *
     * @param $seatCode
     * @param $storageUuid
     * @return mixed
     */
    public function getSeatType($seatCode,$storageUuid){
        $where['sc.seat_code'] = $seatCode;
        $where['sc.storage_uuid'] = $storageUuid;
        $result= $this->alias('sc')
            ->join(['iwh_shelves'=>'sh'],'sc.shelves_uuid = sh.uuid','inner')
            ->where($where)
            ->value('sh.type');
        return $result;

    }

    /**
     * 模糊查询一个物料
     *
     * @param $seatCode
     * @param $storageUuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function findSeatCode($seatCode,$storageUuid){
        return $this->whereLike('seat_code',$seatCode.'%')
            ->whereLike('storage_uuid',$storageUuid)
            ->find();
    }

    /***
     * @param $where
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 寻找站位
     */
    public function findSeatCodeByWhere($where){
        return $this->where($where)->find();
    }
}